#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    int hIndex(vector<int>& citations) {
        int n = citations.size();
        if (citations[n - 1] == 0) return 0;
        int left = 0, right = n - 1;
        while (left < right)
        {
            int mid = (left + right) / 2;
            if (citations[mid] < n - mid) left = mid + 1;
            else right = mid;
        }
        return n - left;
    }
};